//
//  YFHud.m
//  project
//
//  Created by yefei on 2019/4/11.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFHud.h"
#import "JGProgressHUD.h"
#import "YFProgressView.h"

#define DEFAULT_DISMISS_SECONDS (1.5)
#define SUCCESS_DISMISS_SECONDS (2.6)
#define ERROR_DISMISS_SECONDS (2.6)
#define MESSAGE_DISMISS_SECONDS (2.6)

@interface YFHud () <JGProgressHUDDelegate>

/**
 JGProgressHUD
 */
@property (nonatomic, strong)JGProgressHUD *jgpHUD;

/**
 hud 消失后的回调 只执行一次
 */
@property(nonatomic, copy, nullable) void(^dismissedAction)(void);

@end


@implementation YFHud

#pragma mark - 绝对单例
static  YFHud * _hud = nil;
+ (instancetype)hud
{
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _hud = [[[self class] alloc] init];
//        [_hud configureHUD];
    });
    return _hud;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _hud = [super allocWithZone:zone];
    });
    return _hud;
}

- (instancetype)copyWithZone:(NSZone *)zone
{
    return _hud;
}

- (instancetype)mutableCopyWithZone:(NSZone *)zone
{
    return _hud;
}

#pragma mark - Class Methods 显示菊花

/**
 显示菊花 Progress
 默认显示在当前控制器的 View 上
 
 @param text 加载提示语 如：加载中...
 */
+ (void)showProgress:(NSString *)text
{
    [YFHud showProgress:text inView:[UIViewController currentViewController].view];
}

/**
 显示菊花 Progress
 默认显示在当前控制器的 View 上
 开启交互
 
 @param text 加载提示语 如：加载中...
 */
+ (void)showProgressWithInteraction:(NSString *)text
{
    [YFHud showProgressWithInteraction:text inView:[UIViewController currentViewController].view];
}

/**
 显示菊花 Progress，显示在 KeyWindow 上
 
 @param text 加载提示语 如：加载中...
 */
+ (void)showProgressInWindow:(NSString *)text
{
    [YFHud showProgress:text inView:[UIApplication sharedApplication].keyWindow];
}

/**
 显示菊花 Progress，显示在 KeyWindow 上
 开启交互
 
 @param text 加载提示语 如：加载中...
 */
+ (void)showProgressInWindowWithInteraction:(NSString *)text
{
    [YFHud showProgressWithInteraction:text inView:[UIApplication sharedApplication].keyWindow];
}

/**
 显示菊花，指定显示 View

 @param text 加载提示语 如：加载中...
 @param view 指定的 View
 */
+ (void)showProgress:(NSString *)text inView:(UIView *)view
{
    [[YFHud hud] showProgress:text inView:view isInteractionEnabled:NO];
}

/**
 显示菊花，指定显示 View
 开启交互
 
 @param text 加载提示语 如：加载中...
 @param view 指定的 View
 */
+ (void)showProgressWithInteraction:(NSString *)text inView:(UIView *)view
{
    [YFHud hud].jgpHUD.interactionType = JGProgressHUDInteractionTypeBlockNoTouches;
    [[YFHud hud] showProgress:text inView:view isInteractionEnabled:NO];
}

#pragma mark - Class Methods 显示成功信息

/**
 显示成功信息
 默认显示在 当前控制器的 View 上
 默认显示1.5秒
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 */
+ (void)showSuccess:(NSString *)text
{
    [YFHud showSuccess:text dismissAfter:SUCCESS_DISMISS_SECONDS];
}

/**
 显示成功信息
 默认显示在 当前控制器的 View 上
 默认显示1.5秒
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 */
+ (void)showSuccess:(NSString *)text dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showSuccess:text inView:[UIViewController currentViewController].view dismissAfter:seconds];
}

/**
 显示成功信息 显示在 KeyWindow 上
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 */
+ (void)showSuccessInWindow:(NSString *)text
{
    [YFHud showSuccessInWindow:text dismissAfter:SUCCESS_DISMISS_SECONDS];
}

/**
 显示成功信息 显示在 KeyWindow 上
 默认显示1.5秒
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 @param seconds 多少秒消失
 */
+ (void)showSuccessInWindow:(NSString *)text dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showSuccess:text inView:[UIApplication sharedApplication].keyWindow dismissAfter:seconds];
}

/**
 显示成功信息，指定显示 View
 默认显示1.5秒
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 @param view 指定的 View
 */
+ (void)showSuccess:(NSString *)text inView:(UIView *)view
{
    [YFHud showSuccess:text inView:view dismissAfter:SUCCESS_DISMISS_SECONDS];
}

/**
 显示成功信息，指定显示 View
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 @param view 指定的 View
 @param seconds 多少秒消失
 */
+ (void)showSuccess:(NSString *)text inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [[YFHud hud] showSuccess:text inView:view dismissAfter:seconds];
}

#pragma mark - Class Methods 显示错误

/**
 显示错误信息
 默认显示在当前控制器的 View 上
 默认1.5秒后消失
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 */
+ (void)showError:(NSString *)text
{
    [YFHud showError:text dismissAfter:ERROR_DISMISS_SECONDS];
}

/**
 显示错误信息
 默认显示在当前控制器的 View 上
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 @param seconds 多少秒消失
 */
+ (void)showError:(NSString *)text dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showError:text inView:[UIViewController currentViewController].view dismissAfter:seconds];
}

/**
 显示错误信息，显示在 KeyWindow 上
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 */
+ (void)showErrorInWindow:(NSString *)text
{
    [YFHud showErrorInWindow:text dismissAfter:ERROR_DISMISS_SECONDS];
}
/**
 显示错误信息，显示在 KeyWindow 上
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 @param seconds 多少秒消失
 */
+ (void)showErrorInWindow:(NSString *)text dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showError:text inView:[UIApplication sharedApplication].keyWindow dismissAfter:seconds];
}

/**
 显示错误信息 指定显示 View
 默认1.5秒后消失
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 @param view 指定的 View
 */
+ (void)showError:(NSString *)text inView:(UIView *)view
{
    [YFHud showError:text inView:view dismissAfter:ERROR_DISMISS_SECONDS];
}

/**
 显示错误信息 指定显示 View
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 @param view 指定的 View
 @param seconds 多少秒消失
 */
+ (void)showError:(NSString *)text inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [[YFHud hud] showError:text inView:view dismissAfter:seconds];
}

#pragma mark - Class Methods Message消息
/**
 显示 message 信息
 默认显示在 当前控制器的 View 上
 默认1.5后消失
 
 @param message 信息 如：您需要验证手机号
 */
+ (void)showMessage:(NSString *)message
{
    [YFHud showMessage:message dismissAfter:MESSAGE_DISMISS_SECONDS];
}

/**
 显示 message 信息
 默认显示在 当前控制器的 View 上

 @param message 信息 如：您需要验证手机号
 @param seconds 多少秒后消失
 */
+ (void)showMessage:(NSString *)message dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showMessage:message inView:[UIViewController currentViewController].view dismissAfter:seconds];
}

/**
 显示 message 信息 显示在 KeyWindow 上
 默认关闭交互
 默认1.5后关闭
 
 @param message 信息 如：您需要验证手机号
 */
+ (void)showMessageInWindow:(NSString *)message
{
    [YFHud showErrorInWindow:message dismissAfter:MESSAGE_DISMISS_SECONDS];
}
/**
 显示 message 信息 显示在 KeyWindow 上
 默认关闭交互
 
 @param message 信息 如：您需要验证手机号
 @param seconds 多少秒后消失
 */
+ (void)showMessageInWindow:(NSString *)message dismissAfter:(NSTimeInterval)seconds
{
    [YFHud showMessage:message inView:[UIApplication sharedApplication].keyWindow dismissAfter:seconds];
}

/**
 显示 message 信息 指定显示 View
 默认关闭交互
 
 @param message 信息 如：您需要验证手机号
 @param view 指定的 view
 */
+ (void)showmessage:(NSString *)message inView:(UIView *)view
{
    [YFHud showMessage:message inView:view dismissAfter:MESSAGE_DISMISS_SECONDS];
}

/**
 显示 message 信息 指定显示 View
 默认关闭交互
 
 @param message 信息 如：您需要验证手机号
 @param view 指定的 view
 @param seconds 多少秒消失
 */
+ (void)showMessage:(NSString *)message inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [[YFHud hud] showMessage:message inView:view dismissAfter:seconds];
}

#pragma mark - Class Methods 消失
/**
 消失
 */
+ (void)dismiss
{
    [[YFHud hud] dismiss];
}

/**
 带动画的消失
 */
+ (void)dismissAnimated {
    [[YFHud hud] dismissAnimated];
}

#pragma mark - Class Methods 消失回调
/**
 消失回调
 
 @param dismissedAction 回调 Block
 */
+  (void)dismissedAction:(void(^)(void))dismissedAction {
    if (dismissedAction) {
        [YFHud hud].dismissedAction = [dismissedAction copy];
    }
}


#pragma mark - 显示Progress

/**
 显示菊花 Progress，指定显示View

 @param text 加载提示语 如：加载中...
 @param view 指定的 View
 */
- (void)showProgress:(NSString *)text inView:(UIView *)view isInteractionEnabled:(BOOL)isEnabled
{
    [self dismiss];
    self.jgpHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    self.jgpHUD.animation = [JGProgressHUDFadeAnimation animation];
    self.jgpHUD.delegate = self;
    self.jgpHUD.contentInsets = UIEdgeInsetsMake(20, 40, 20, 40);
    self.jgpHUD.textLabel.text = text;
    self.jgpHUD.interactionType = isEnabled ? JGProgressHUDInteractionTypeBlockTouchesOnHUDView : JGProgressHUDInteractionTypeBlockAllTouches;
    [self.jgpHUD showInView:view animated:YES];
}

#pragma mark 显示成功

/**
 显示成功信息，指定显示 View
 默认关闭交互
 
 @param text 成功提示语，如：Success!
 @param view 指定的 View
 @param seconds 多少秒消失
 */
- (void)showSuccess:(NSString *)text inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [self dismiss];
    self.jgpHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    self.jgpHUD.animation = [JGProgressHUDFadeAnimation animation];
    self.jgpHUD.delegate = self;
    self.jgpHUD.contentInsets = UIEdgeInsetsMake(20, 40, 20, 40);
    self.jgpHUD.textLabel.text = text;
    self.jgpHUD.interactionType = JGProgressHUDInteractionTypeBlockTouchesOnHUDView;
    self.jgpHUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    [self.jgpHUD showInView:view animated:YES];
    [self.jgpHUD dismissAfterDelay:seconds animated:YES];
}

#pragma mark - 显示失败

/**
 显示错误信息 指定显示 View
 默认关闭交互
 
 @param text 错误提示语，如：加载失败！
 @param view 指定的 View
 @param seconds 多少秒消失
 */
- (void)showError:(NSString *)text inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [self dismiss];
    self.jgpHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    self.jgpHUD.animation = [JGProgressHUDFadeAnimation animation];
    self.jgpHUD.delegate = self;
    self.jgpHUD.contentInsets = UIEdgeInsetsMake(20, 40, 20, 40);
    self.jgpHUD.textLabel.text = text;
    self.jgpHUD.interactionType = JGProgressHUDInteractionTypeBlockTouchesOnHUDView;
    self.jgpHUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    [self.jgpHUD showInView:view animated:YES];
    [self.jgpHUD dismissAfterDelay:seconds animated:YES];
}

#pragma mark - 显示信息
/**
 显示 message 信息 指定显示 View
 默认关闭交互
 
 @param message 信息 如：您需要验证手机号
 @param view 指定的 view
 @param seconds 多少秒消失
 */
- (void)showMessage:(NSString *)message inView:(UIView *)view dismissAfter:(NSTimeInterval)seconds
{
    [self dismiss];
    self.jgpHUD = [[JGProgressHUD alloc] initWithStyle:(JGProgressHUDStyleDark)];
    self.jgpHUD.animation = [JGProgressHUDFadeAnimation animation];
    self.jgpHUD.delegate = self;
    self.jgpHUD.contentInsets = UIEdgeInsetsMake(20, 40, 20, 40);
    self.jgpHUD.textLabel.text = message;
    self.jgpHUD.interactionType = JGProgressHUDInteractionTypeBlockTouchesOnHUDView;
    self.jgpHUD.indicatorView = nil;
    [self.jgpHUD showInView:view animated:YES];
    [self.jgpHUD dismissAfterDelay:seconds animated:YES];
}


#pragma mark - 消失
- (void)dismiss {
    
    if (self.jgpHUD.isVisible) {
        [self.jgpHUD dismissAnimated:NO];
    }
}

- (void)dismissAnimated {
    if (self.jgpHUD.isVisible) {
        [self.jgpHUD dismissAnimated:YES];
    }
}



#pragma mark - JGProgressHUDDelegate

/**
 Called before the HUD will appear.
 @param view The view in which the HUD is presented.
 */
- (void)progressHUD:(JGProgressHUD *__nonnull)progressHUD willPresentInView:(UIView *__nonnull)view {
    
}

/**
 Called after the HUD appeared.
 @param view The view in which the HUD is presented.
 */
- (void)progressHUD:(JGProgressHUD *__nonnull)progressHUD didPresentInView:(UIView *__nonnull)view {
    
}

/**
 Called before the HUD will disappear.
 @param view The view in which the HUD is presented and will be dismissed from.
 */
- (void)progressHUD:(JGProgressHUD *__nonnull)progressHUD willDismissFromView:(UIView *__nonnull)view {
    
}

/**
 Called after the HUD has disappeared.
 @param view The view in which the HUD was presented and was be dismissed from.
 */
- (void)progressHUD:(JGProgressHUD *__nonnull)progressHUD didDismissFromView:(UIView *__nonnull)view {
    
    // 执行回调
    if (_dismissedAction) {
        _dismissedAction();
        self.dismissedAction = nil;
    }
    
}



@end
